0

我正在对 Watson Alchemy API 进行以下查询

$response = $client->request('GET','https://gateway-a.watsonplatform.net/calls/url/URLGetEmotion?apikey='.env('WATSON_ALCHEMY_API_KEY').'&url='.$url.'&showSourceText=1&sourceText=cleaned&outputMode=json');
$item = json_decode($response->getBody(), true);

如果我打印结果,$item['text']我会得到以下信息:

"""
                                                            \n
 - Pioneer kann zunächst eine 51-Prozent-Beteiligung am Projekt erwerben, indem es innerhalb von drei Jahren 1,25 Millionen Kanadische Dollar in Explorationsarbeiten investiert. \n
 - Zurzeit werden historische Explorationsarbeiten zusammengestellt, um ein Programm für 2016 zu planen. \n
 - ILC stärkt seine Nordamerika-Strategie, mehrere Lithiumprojekte durch strategische Partnerschaften für potenzielle Erschließungssynergien weiterzuentwickeln. \n 

[...]

"""

我无法摆脱这些""",当我收到错误时它们会打扰我:

Undefined index: body

文本在另一个函数中变成了正文。如果我转储数组,则索引body在那里......:

"body" => """
      Die ägyptische Regierung bemüht sich um Ordnung und Stabilität im Land. Sie geht aber auch brutal gegen Kritiker vor, wie Amnesty International sagt. Immer häufiger verschwinden junge Menschen einfach spurlos.          \n 
[...]
"""

有没有人遇到过这个?谢谢你。

更新:

var_dump 的$response

/home/vagrant/Projects/blick/app/Http/Controllers/NewsController.php:142:
object(GuzzleHttp\Psr7\Response)[220]
  private 'reasonPhrase' => string 'OK' (length=2)
  private 'statusCode' => int 200
  private 'headers' => 
    array (size=17)
      'Server' => 
        array (size=1)
          0 => string 'nginx' (length=5)
      'Date' => 
        array (size=1)
          0 => string 'Thu, 14 Jul 2016 15:54:41 GMT' (length=29)
      'Content-Type' => 
        array (size=1)
          0 => string 'application/json' (length=16)
      'Content-Length' => 
        array (size=1)
          0 => string '6940' (length=4)
      'Connection' => 
        array (size=1)
          0 => string 'keep-alive' (length=10)
      'Cache-Control' => 
        array (size=1)
          0 => string 'no-cache' (length=8)
      'X-AlchemyAPI-CurrentVersion' => 
        array (size=1)
          0 => string '12.52a' (length=6)
      'X-AlchemyAPI-Error-Msg' => 
        array (size=1)
          0 => string 'unsupported-text-language' (length=25)
      'X-AlchemyAPI-Key' => 
        array (size=1)
          0 => string 'api-key-removed' (length=40)
      'X-AlchemyAPI-Params' => 
        array (size=1)
          0 => string 'sentiment=0&knowledgeGraph=0&detectedLanguage=german&submitLanguage=detect' (length=74)
      'X-AlchemyAPI-Raw-Content-Size' => 
        array (size=1)
          0 => string '106952' (length=6)
      'X-AlchemyAPI-Requested-URL' => 
        array (size=1)
          0 => string 'http://www.srf.ch/news/international/amnesty-wirft-aegypten-brutale-willkuer-gegen-regimekritiker-vor' (length=101)
      'X-AlchemyAPI-Retrieved-URL' => 
        array (size=1)
          0 => string 'http://www.srf.ch/news/international/amnesty-wirft-aegypten-brutale-willkuer-gegen-regimekritiker-vor' (length=101)
      'X-AlchemyAPI-Status' => 
        array (size=1)
          0 => string 'ERROR' (length=5)
      'X-AlchemyAPI-Text-Content-Size' => 
        array (size=1)
          0 => string '6093' (length=4)
      'X-AlchemyAPI-Total-Transactions' => 
        array (size=1)
          0 => string '1' (length=1)
      'Access-Control-Allow-Origin' => 
        array (size=1)
          0 => string '*' (length=1)
  private 'headerNames' => 
    array (size=17)
      'server' => string 'Server' (length=6)
      'date' => string 'Date' (length=4)
      'content-type' => string 'Content-Type' (length=12)
      'content-length' => string 'Content-Length' (length=14)
      'connection' => string 'Connection' (length=10)
      'cache-control' => string 'Cache-Control' (length=13)
      'x-alchemyapi-currentversion' => string 'X-AlchemyAPI-CurrentVersion' (length=27)
      'x-alchemyapi-error-msg' => string 'X-AlchemyAPI-Error-Msg' (length=22)
      'x-alchemyapi-key' => string 'X-AlchemyAPI-Key' (length=16)
      'x-alchemyapi-params' => string 'X-AlchemyAPI-Params' (length=19)
      'x-alchemyapi-raw-content-size' => string 'X-AlchemyAPI-Raw-Content-Size' (length=29)
      'x-alchemyapi-requested-url' => string 'X-AlchemyAPI-Requested-URL' (length=26)
      'x-alchemyapi-retrieved-url' => string 'X-AlchemyAPI-Retrieved-URL' (length=26)
      'x-alchemyapi-status' => string 'X-AlchemyAPI-Status' (length=19)
      'x-alchemyapi-text-content-size' => string 'X-AlchemyAPI-Text-Content-Size' (length=30)
      'x-alchemyapi-total-transactions' => string 'X-AlchemyAPI-Total-Transactions' (length=31)
      'access-control-allow-origin' => string 'Access-Control-Allow-Origin' (length=27)
  private 'protocol' => string '1.1' (length=3)
  private 'stream' => 
    object(GuzzleHttp\Psr7\Stream)[218]
      private 'stream' => resource(17, stream)
      private 'size' => null
      private 'seekable' => boolean true
      private 'readable' => boolean true
      private 'writable' => boolean true
      private 'uri' => string 'php://temp' (length=10)
      private 'customMetadata' => 
        array (size=0)
          empty
array:7 [
  "status" => "OK"
  "usage" => "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html"
  "url" => "http://www.usatoday.com/story/news/world/2016/07/14/refugees-europe-say-they-fear-terrorists-among-them/87008334/"
  "totalTransactions" => "1"
  "language" => "english"
  "text" => """
    BERLIN — Saif Ali grew nervous when he met his six Syrian bunkmates in a Munich refugee camp after finally making it to Germany late last year.\n
    “They were strong supporters of the Nusra Front,” said the Iraqi refugee, referring to the al-Qaeda-affiliated terrorist group fighting in the Syrian conflict. “I was praying five times a day, to hide my beliefs from them. They did not force me to, but I did not feel secure.”\n
    Ali, 21, is an atheist and said his lack of religion was one reason he fled Iraq. He worried that if his bunkmates knew, they would consider him an apostate and beat him up — or worse.\n
    It's a common story in Europe these days. Many refugees have detailed experiences similar to Ali’s, encountering extremists among the estimated 1 million migrants who made the journey into Europe from the Middle East last year.\n
    After the Islamic State’s attacks in Paris in November and in Brussels in March, European concerns that terrorists could be arriving as refugees have taken on new importance, especially as more migrants arrive. Similar concerns are echoed in the United States, notably by Republican presidential candidate Donald Trump.\n
    A survey by the Washington-based Pew Research Center released this week found about half to three-quarters of Europeans, depending on the country, said the wave of refugees raises the risk of terrorist attacks in their countries. Respondents said they fear the newcomers — even as some refugees say the same thing.\n
    “I see many extremists,” said Bader Khaishah, 28, a Syrian refugee at a Munich refugee camp. “I cannot be certain, but they have strong tendencies (toward militancy). I can feel this from the extremist tunes on their mobile phones, their injuries and their reaction when discussing the incidents in our region.”\n
    Frontex, the European Union's border control agency, said the Paris terrorist attacks that killed 130 people demonstrated that terrorists can mingle among the flow of migrants into Europe. "Two of the terrorists involved in the attacks had previously irregularly entered through Leros (a Greek island) and had been registered by the Greek authorities,” Frontex said in its 2016 risk analysis.\n
    German Chancellor Angela said this week  that militant groups are sneaking people into Europe among the refugees. "The refugee wave (last year) was used by some to smuggle in terrorists," she said.\n
    Germany said it is investigating about 40 cases of suspected radicals posing as refugees. In June, German police arrested three Syrian men on allegations of planning a terror attack in Dusseldorf for the Islamic State. A fourth man allegedly linked to the plot is in custody in France.\n
    Jamal Jabur, 32, an Iraqi refugee in Esslingen, Germany, said he met three men who claimed they previously fought for the Islamic State. Two of the men, from Ramadi, Iraq, were forced into the militant group, he said. But a third, from the Iraqi city of Mosul, believed in the Islamic State’s cause and often chastised anyone who didn’t follow the group’s harsh interpretation of Islam.\n
    “The man from Mosul is a dangerous person,” Jabur said. “Once, the man from Ramadi and I talked with a German woman, and the Mosul member said this is wrong, and that prophet Mohammed disapproves.”\n
    Jabur said he never met anyone affiliated with the Islamic State, also known is ISIS or ISIL, when he lived in Baghdad from 2003 until 2015.\n
    "Within a few days in the refugees' camp in Germany, I met three former ISIS members," he said, shaking his head. “There are lots of them. Many are escaping the service with ISIS, but they seem to be dangerous. I felt afraid to tell the camp administration about them.”\n
    Some extremists see their mission as converting Christians into Muslims, Ali said about a fighter from the Nusra Front militant group who was forced out of Syria after the Islamic State invaded his town.\n
    “One of this man’s goals after getting the residence (permit) was to spread Islam. He said, 'Europe will become Muslim, we will Islamicize (them),'” Ali said.\n
    Others who share that goal believe they are being helpful, Ali added.\n
    "They are happy with the German attitude of welcoming other cultures and see it a suitable environment to spread Islam," he said. "They say, 'Germans are good, and we should save them.'"\n
    Nabeel reported from Istanbul.\n
    """
  "docEmotions" => array:5 [
    "anger" => "0.897111"
    "disgust" => "0.503957"
    "fear" => "0.804622"
    "joy" => "0.041228"
    "sadness" => "0.043859"
  ]
]
4

1 回答 1

0

经过一番挣扎,我找到了答案。问题来自于简单地\n被奇怪地解析json_decode()

$text = str_replace("\n", '', $item['text']);

我摆脱了这个问题。

于 2016-07-14T20:44:30.297 回答